package com.acwing.partition43;

import java.io.*;

/**
 * @author rkc
 * @date 2022/6/23 15:13
 */
public class AC4274后缀表达式 {

    private static final int N = 25;
    private static String[] op = new String[N];
    private static int[] l = new int[N], r = new int[N];
    private static boolean[] st = new boolean[N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        for (int i = 1; i <= n; i++) {
            String[] ss = reader.readLine().split(" ");
            op[i] = ss[0];
            l[i] = Integer.parseInt(ss[1]);
            r[i] = Integer.parseInt(ss[2]);
            if (l[i] != -1) st[l[i]] = true;
            if (r[i] != -1) st[r[i]] = true;
        }
        int root = 0;
        for (int i = 1; i <= n; i++) {
            if (!st[i]) {
                root = i;
                break;
            }
        }
        dfs(root);
        writer.flush();
    }

    private static void dfs(int u) throws IOException {
        writer.write('(');
        if (l[u] != -1 && r[u] != -1) {
            dfs(l[u]);
            dfs(r[u]);
            writer.write(op[u]);
        } else if (l[u] == -1 && r[u] == -1) {
            writer.write(op[u]);
        } else {
            writer.write(op[u]);
            dfs(r[u]);
        }
        writer.write(')');
    }
}
